package cs294s.liveradar.data;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.json.simple.JSONObject;


import com.google.android.maps.GeoPoint;

import cs294s.liveradar.service.FeedManager;


public class Blip {
	long id = -1;
	GeoPoint location;
	String description;
	Date eventTime;
	Date createdTime;
	Person author;
	Set<String> tags = new HashSet<String>();;
	int nRecipients;
	
	Filter f;

	public Blip() {

	}

	public Blip(JSONObject json) {
		addTags((String)json.get("tags"));
		removeTag(FeedManager.LIVERADAR_TAG);
		setDescription((String)json.get("desc"));
		Long time_long = (Long)json.get("time");
		setEventTime(new Date(time_long));
		setLocation(new GeoPoint(((Long)json.get("lat")).intValue(), ((Long)json.get("lon")).intValue()));

	}
	
	public void setFilter(Filter f) {
		this.f = f;
	}

	public void setId(long id) {this.id = id;}
	
	public void setTags(HashSet<String> tags) {this.tags = tags;}

	public void addTag(String tag) { tags.add(tag); }

	public void removeTag(String tag) { tags.remove(tag); }

	public void setLocation(GeoPoint location) { this.location = location; }

	public void setEventTime(Date time) { this.eventTime = time; }

	public void setAuthor(Person author) { this.author = author; }

	public void setDescription(String desc) { this.description = desc; }

	public void addTags(String tags) { 
		String[] tags_arr =  tags.split(",");
		for(String t : tags_arr) {
			addTag(t.trim());
		}
	}


	public String tagsToString() {
		String result = "";
		for(String s : tags) {
			result += s + ", ";
		}
		result = result.substring(0, result.length()-2);
		return result;
	}

	public String[] tagsToStringArr() {
		return (String[])tags.toArray(new String[tags.size()]);
	}

	@SuppressWarnings("unchecked")
	public JSONObject toJSON() {

		JSONObject j = new JSONObject();
		j.put("tags", tagsToString());
		j.put("desc", description);
		j.put("time", eventTime.getTime());
		j.put("lat", location.getLatitudeE6());
		j.put("lon", location.getLongitudeE6());
		return j;
	}

	public long getId() {
		return id;
	}
	
	public GeoPoint getLocation() { return location; }

	public Date getEventTime() { return eventTime; }

	public Person getAuthor() { return author;}

	public String getDescription() { return description;}

	public Filter getFilter() { return f; }
}
